package com.maas.util.mybatis.jpa.littlenb.meta;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.maas.util.mybatis.jpa.littlenb.annotation.entity.CatTableFlag;
import com.maas.util.mybatis.jpa.littlenb.common.JackPersistentUtil;


/**
 * 从 mybatis-jpa-jack 移植过来的
 * 持久化元数据类型
 *
 * @author svili
 * @data 2017年5月6日
 *
 */
public class PersistentMeta {

	/** 持久化对象 类型 */
	private Class<?> type;

	/** 表名 */
	private String tableName;

	/** {@link javax.persistence.Entity} */
	private String entityName;

	private String flat = "";

	private String tableNameFlag;

	private boolean isCatTable = false;

	private String flagTableName = "";

	// private String primaryKey;

	/** 主键column元数据 */
	private MybatisColumnMeta primaryColumnMeta;

	/** 列名集 */
	private List<String> columnNameCollection;

	private String columnNames;

	/** column元数据集 {key-fieldName} */
	private Map<String, MybatisColumnMeta> columnMetaMap;

	public PersistentMeta(Class<?> type) {
		this.type = type;
		persistence();
	}

	private void persistence() {
		tableName = JackPersistentUtil.getTableName(type);
		entityName = JackPersistentUtil.getEntityName(type);
		primaryColumnMeta = new MybatisColumnMeta(JackPersistentUtil.getPrimaryField(type),this);
		tableNameFlag = tableName;
        // 获取注解对象
        if (type.isAnnotationPresent(CatTableFlag.class)) {
            CatTableFlag catTable = type.getAnnotation(CatTableFlag.class);
            if (!"".equals(catTable.value().trim())) {
                flat = catTable.value();
                tableNameFlag+= "_${" + catTable.value() + "}";
                isCatTable = true;
            }
        }
        flagTableName =  tableName;
        if(isCatTable)
        {
            flagTableName += "_${flag}";
        }
		// 持久化字段集
		List<Field> fields = JackPersistentUtil.getPersistentFields(type);

		// 初始化集合
		columnNameCollection = new ArrayList<String>();
		columnMetaMap = new LinkedHashMap<String, MybatisColumnMeta>();
		StringBuilder columnNamesTemp = new StringBuilder();

		for (Field field : fields) {
			//静态不需要处理
			if(Modifier.isStatic(field.getModifiers()))
			{
				continue;
			}
			MybatisColumnMeta columnMeta = new MybatisColumnMeta(field,this);
			columnMetaMap.put(field.getName(), columnMeta);
			columnNameCollection.add(columnMeta.getColumnName());
			columnNamesTemp.append(",").append(columnMeta.getColumnName());
		}
		columnNames = columnNamesTemp.substring(1);
	}

	// getter

	public Class<?> getType() {
		return type;
	}

	public String getTableName() {
		return tableName;
	}

	public String getEntityName() {
		return entityName;
	}

	public MybatisColumnMeta getPrimaryColumnMeta() {
		return primaryColumnMeta;
	}

	public List<String> getColumnNameCollection() {
		return columnNameCollection;
	}

	public String getColumnNames() {
		return columnNames;
	}

	public Map<String, MybatisColumnMeta> getColumnMetaMap() {
		return columnMetaMap;
	}

    public String getFlat()
    {
        return flat;
    }

    public void setFlat(String flat)
    {
        this.flat = flat;
    }

    public String getTableNameFlag()
    {
        return tableNameFlag;
    }

    public void setTableNameFlag(String tableNameFlag)
    {
        this.tableNameFlag = tableNameFlag;
    }

    public boolean isCatTable()
    {
        return isCatTable;
    }

    public void setCatTable(boolean isCatTable)
    {
        this.isCatTable = isCatTable;
    }

    public String getFlagTableName()
    {
        return flagTableName;
    }

    public void setFlagTableName(String flagTableName)
    {
        this.flagTableName = flagTableName;
    }

}
